package notifiers

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

/**
 * @Author: way lee
 * @Date: 2024/9/10 09:07
 * @Desc:
 */

type SMSConfig struct {
	APIKey    string `json:"api_key"`
	APISecret string `json:"api_secret"`
	Sender    string `json:"sender"`
	Recipient string `json:"recipient"`
}

func SendSMS(config []SMSConfig, message string) error {
	payload := map[string]interface{}{
		"api_key":    config[0].APIKey,
		"api_secret": config[0].APISecret,
		"sender":     config[0].Sender,
		"recipient":  config[0].Recipient,
		"message":    message,
	}
	body, err := json.Marshal(payload)
	if err != nil {
		return fmt.Errorf("error marshalling payload: %w", err)
	}

	req, err := http.NewRequest("POST", "https://api.smsprovider.com/send", bytes.NewBuffer(body))
	if err != nil {
		return fmt.Errorf("创建 SMS HTTP 请求错误: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return fmt.Errorf("发送 SMS HTTP 请求异常: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
	}

	return nil
}
